<!-- 员工佣金发放总表-->
<template>
    <div id="serach">
        <el-form :inline="true" :model="ruleForm" class="demo-form-inline">
            <el-form-item label="报表名称：">
                <el-input placeholder="请输入报表名称" v-model.trim="ruleForm.reportName" maxlength="20"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchList" id="monitorEnter">查询</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="tableData" border>
            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
            <el-table-column label="报表名称" prop="reportFormName" align="center"></el-table-column>
            <el-table-column label="月份" prop="month" align="center"></el-table-column>
            <el-table-column label="总佣金" prop="amountTotal" align="center" :formatter="initCash"></el-table-column>
            <el-table-column label="本次发放" prop="amountThisMonth" align="center" :formatter="initCash"></el-table-column>
            <el-table-column label="年底发放" prop="amountThisYear" align="center" :formatter="initCash"></el-table-column>
            <el-table-column label="是否发放佣金" prop="grantStatus" align="center">
                <template slot-scope="scope">
                    {{scope.row.grantStatus == 0? '否':'是'}}
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" @click="seehander(scope.row)">查看</el-button>
                    <el-button type="text" :class="scope.row.grantStatus == 0? '':'lock'" @click="sendCommission(scope.row)">{{scope.row.grantStatus ? "已发放":"发放佣金"}}</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagebar">
            <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page.sync="ruleForm.pageNum"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="ruleForm.pageSize"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :total="totalSize">
            </el-pagination>
        </div>

    </div>
</template>
<script>
    import api from '@/api/apiPath'

    export default {
        name: 'staffCommission',
        title:'员工佣金发放总表',
        data() {
            return {
                ruleForm: {
                    reportName: undefined,
                    pageNum: 1,
                    pageSize: 10,
                },
                totalSize: 0,
                tableData: [],
            }
        },

        methods: {
            initCash(row, column, cellValue, index) {
                if (cellValue !== 0) {
                    return cellValue.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                }
                if (cellValue == 0) {
                    return "0.00"
                }
            },
            searchList() {
                this.ruleForm.pageNum = 1
                this.getlist()
            },
            handleCurrentChange() {
                this.getlist()
            },
            handleSizeChange(val) {
                this.ruleForm.pageSize = val
                this.getlist();
            },
            seehander(val) {
                this.$router.push({path: "./staffCommissionView", query: {id: val.id, month: val.month,beforeAdjust:val.grantStatus}})
            },
            getlist() {
                this.axios.post(api.GRANTMONTHLIST, this.ruleForm).then((res) => {
                    this.tableData = res.data.data.data;
                    this.totalSize = res.data.data.total;
                })
            },
            sendCommission(row) {
                if (!row.grantStatus) {
                    this.$confirm('是否发放佣金?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.axios.post(api.sendCommission, {
                            id: row.id
                        }).then((res) => {
                            this.$message({
                                message: "发放成功",
                                type: 'success'
                            });
                            this.getlist()
                        })
                    });
                }
            }
        },
        created() {
            this.getlist()
        },
        activated(){
           this.searchList()
        }
    }

</script>


<style lang="stylus" scoped>
    .lock {
        color: #cccccc;
        cursor: not-allowed;
    }
</style>
